#pragma once
#include <iostream>
#include <functional>

// 计算任务
class CalTask
{
    // using func_t = std::function<void(int, int)>;

public:
    // 用于消费者定义一个类然后接收被弹出的任务
    CalTask()
    {
    }
    // 用于生产者随机生成两个数并构造一个类
    CalTask(int x, int y, char symbol) : m_x(x), m_y(y), m_symbol(symbol)
    {
    }
    // 用于生产者打印要计算的式子
    std::string Print()
    {
        return std::to_string(m_x) + m_symbol + std::to_string(m_y);
    }
    // 用于消费者计算式子的结果
    std::string Calculate()
    {
        int result;
        switch (m_symbol)
        {
        case '+':
            result = m_x + m_y;
            break;
        case '-':
            result = m_x - m_y;
            break;
        case '*':
            result = m_x * m_y;
            break;
        case '/':
            if (m_y == 0)
            {
                std::cerr << "div zero error!" << std::endl;
                result = -1;
            }
            else
            {
                result = m_x / m_y;
            }
            break;
        case '%':
            if (m_y == 0)
            {
                std::cerr << "mod zero error!" << std::endl;
                result = -1;
            }
            else
            {
                result = m_x % m_y;
            }
            break;
        default:
            break;
        }
        return Print() + "=" + std::to_string(result);
    }

private:
    int m_x;
    int m_y;
    char m_symbol;
    // func_t m_func;
};

// 保存任务
class SaveTask
{
public:
    SaveTask() {}
    SaveTask(std::string message) : m_message(message)
    {
    }
    void Save()
    {
        FILE *fp = fopen("text.txt", "a+");
        if (!fp)
        {
            std::cerr << "fopen fail" << std::endl;
            return;
        }
        fputs(m_message.c_str(), fp);
        fputs("\n", fp);
        fclose(fp);
    }

private:
    std::string m_message;
};